﻿#include "MirCheckBox.h"
#include "../MirSounds/SoundManager.h"

using namespace Client::MirSounds;

namespace Client::MirControls
{

    int MirCheckBox::getTickedIndex() const
    {
        return _tickedIndex;
    }

    void MirCheckBox::setTickedIndex(int value)
    {
        _tickedIndex = value;
    }

    int MirCheckBox::getUnTickedIndex() const
    {
        return _untickedIndex;
    }

    void MirCheckBox::setUnTickedIndex(int value)
    {
        _untickedIndex = value;
    }

    bool MirCheckBox::getChecked() const
    {
        return _checked;
    }

    void MirCheckBox::setChecked(bool value)
    {
        _checked = value;
        setIndex(value ? getTickedIndex() : getUnTickedIndex());
        Redraw();
    }

    bool MirCheckBox::getCenterLabelText() const
    {
        return _center;
    }

    void MirCheckBox::setCenterLabelText(bool value)
    {
        _center = value;
        if (_center)
        {
            _label->setSize(getSize());
            _label->setDrawFormat(TextFormatFlags::HorizontalCenter | TextFormatFlags::VerticalCenter);
        }
        else
        {
            _label->setAutoSize(true);
        }
    }

    void MirCheckBox::setLabelText(const std::string &value)
    {
        if (_label == nullptr || _label->isDisposed())
        {
            return;
        }
        _label->setText(value);
        _label->setVisible(!value.empty());
    }

    MirCheckBox::MirCheckBox()
    {
        setTickedIndex(-1);
        setUnTickedIndex(-1);
        setIndex(-1);
        setHoverIndex(-1);
        setPressedIndex(-1);
        setSound(SoundList::ButtonB);
        Click += MirCheckBox_Click;

        _label = new MirLabel();
        _label->setAutoSize(true);
        _label->setNotControl(true);
        Point tempVar(15, -2);
        _label->setLocation(&tempVar);
        _label->setParent(this);
    }

    void MirCheckBox::MirCheckBox_Click(std::any sender, EventArgs *e)
    {
        setChecked(!getChecked());
        if (getChecked())
        {
            setIndex(getTickedIndex());
        }
        else
        {
            setIndex(getUnTickedIndex());
        }
        Redraw();
    }
}
